package com.che.libcommon.utils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;


public class JsonKit {
  public static final Gson GSON = new Gson();



  public static <T> List<T> asList(JsonObject jsonObject, String path, Class<T> clazz) {
    String[] paths = path.split("\\.");
    JsonObject o = jsonObject;
    for (int i = 0; i < paths.length - 1; i++) {
      o = o.getAsJsonObject(paths[i]);
    }
    JsonArray jsonArray = o.getAsJsonArray(paths[paths.length - 1]);
    Class<T[]> clazzArray = (Class<T[]>) ((T[]) Array.newInstance(clazz, 0)).getClass();
    T[] objectArray = GSON.fromJson(jsonArray, clazzArray);
    return Arrays.asList(objectArray);
  }
}
